package com.weiiew.backend.operationUtils;


import com.weiiew.backend.entity.enums.ExecutorState;
import com.weiiew.backend.entity.status.RecorderStatus;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.helpers.MessageFormatter;
import org.springframework.messaging.simp.SimpMessagingTemplate;

/**
 * @author 魏锴
 * @since 2025/5/3
 */
@Slf4j
public class RecorderCommunicationTool {
    private final SimpMessagingTemplate template;
    private final String channel;
    private long lastSendTime = 0;
    private RecorderStatus status = new RecorderStatus();

    /**
     * 构造方法
     */
    public RecorderCommunicationTool() {
        this.template = null;
        this.channel = null;
    }

    /**
     * 构造方法
     *
     * @param template SimpMessagingTemplate实例
     * @param channel  频道
     */
    public RecorderCommunicationTool(SimpMessagingTemplate template, String channel) {
        this.template = template;
        this.channel = channel;
    }

    /**
     * 发送消息
     *
     * @param state      当前执行器状态
     * @param recordSize 当前操作记录的大小
     * @param message    需要发送的消息，含有{}
     * @param args       需要填充进message的变量
     */
    public void sendMessage(ExecutorState state, int recordSize, String message, Object... args) {
        String formatted = MessageFormatter.arrayFormat(message, args).getMessage();
        sendMessage(state, recordSize, formatted);
    }

    /**
     * 发送消息
     *
     * @param state      当前执行器状态
     * @param recordSize 当前操作记录的大小
     * @param message    需要发送的消息
     */
    public void sendMessage(ExecutorState state, int recordSize, String message) {
        log.info("当前执行器状态{}, {}", state, message);
        if (template == null || channel == null) return;
        status.addMessage(message);
        if (System.currentTimeMillis() - lastSendTime >= 100) {
            innerSendMessage(state, recordSize);
        }
    }

    /**
     * 强制发送消息
     *
     * @param state      当前执行器状态
     * @param recordSize 当前操作记录的大小
     * @param message    需要发送的消息，含有{}
     * @param args       需要填充进message的变量
     */
    public void focusedSendMessage(ExecutorState state, int recordSize, String message, Object... args) {
        String formatted = MessageFormatter.arrayFormat(message, args).getMessage();
        focusedSendMessage(state, recordSize, formatted);
    }

    /**
     * 强制发送消息
     *
     * @param state      当前执行器状态
     * @param recordSize 当前执行的操作index
     * @param message    需要发送的消息
     */
    public void focusedSendMessage(ExecutorState state, int recordSize, String message) {
        log.info("当前执行器状态{}, {}", state, message);
        if (template == null || channel == null) return;
        status.addMessage(message);
        innerSendMessage(state, recordSize);
    }

    /**
     * 真正发送消息的方法
     *
     * @param state      当前状态
     * @param recordSize 当前操作记录的大小
     */
    private synchronized void innerSendMessage(ExecutorState state, int recordSize) {
        lastSendTime = System.currentTimeMillis();
        status.setState(state);
        status.setRecordSize(recordSize);
        assert template != null;
        assert channel != null;
        template.convertAndSend(channel, status);
        status = new RecorderStatus();
    }
}
